package dst.ass1.jpa.model.impl;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import dst.ass1.jpa.model.IMetadata;
import dst.ass1.jpa.model.ITask;
import dst.ass1.jpa.model.ITaskProcessing;
import dst.ass1.jpa.model.IUser;

@Entity
@NamedQueries({
	  @NamedQuery(
	    //1.2.a.
	    //Find all Finished tasks (i.e., all tasks with task processing status FINISHED).
	    name="allFinishedTasks",
	    query="SELECT DISTINCT t FROM Task t " +    //als tasks
	        "JOIN t.taskProcessing p " +			//join with TaskProcessing
	        "WHERE p.status = 'FINISHED'"  //status have to be FINISHED (link to enum)
	  )
})
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "metadata_id" }) })
public class Task implements ITask{

	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
	private Integer workUnits;
	
	private Integer processingTime;
	
	private boolean isPaid;
	
	@OneToOne(targetEntity = Metadata.class,
             cascade = CascadeType.ALL,
             optional = false,
             fetch=FetchType.EAGER)  //to-one properties are loaded eagerly (as loading them is cheap)
	private IMetadata metadata;
	
	@ManyToOne(targetEntity = User.class,cascade = { CascadeType.PERSIST, CascadeType.MERGE }, optional = false, fetch=FetchType.EAGER)  //to-one properties are loaded eagerly (as loading them is cheap))
	private IUser user;
	
	
	@OneToOne(targetEntity = TaskProcessing.class,
            cascade = CascadeType.ALL,
            optional = false,
            fetch=FetchType.EAGER) //to-one properties are loaded eagerly (as loading them is cheap)
	private ITaskProcessing taskProcessing;
	
	public Task (){}
	
	@Override
	public Long getId() {
		return id;
	}
	
	@Override
	public void setId(Long id) {
		this.id = id;
	}
	
	@Override
	public Integer getAssignedWorkUnits() {
		return workUnits;
	}
	
	@Override
	public void setAssignedWorkUnits(Integer workUnits) {
		this.workUnits = workUnits;
	}
	
	@Override
	public Integer getProcessingTime() {
		return processingTime;
	}
	
	@Override
	public void setProcessingTime(Integer processingTime) {
		this.processingTime = processingTime;
	}
	
	@Override
	public boolean isPaid() {
		return isPaid;
	}
	
	@Override
	public void setPaid(boolean isPaid) {
		this.isPaid = isPaid;
	}
	
	@Override
	public IMetadata getMetadata() {
		return metadata;
	}
	
	@Override
	public void setMetadata(IMetadata metadata) {
		this.metadata = metadata;
	}
	
	@Override
	public IUser getUser() {
		return user;
	}
	
	@Override
	public void setUser(IUser user) {
		this.user = user;
	}
	
	@Override
	public ITaskProcessing getTaskProcessing() {
		return taskProcessing;
	}
	
	@Override
	public void setTaskProcessing(ITaskProcessing processing) {
		this.taskProcessing = processing;
	}	

}
